﻿
using System;
using System.Web;

namespace WS.EKA.Utilities.Core
{
    public class SessionRepository : ISessionRepository
    {
        public object this[string key]
        {
            get
            {
                return HttpContext.Current.Session[key];
            }
            set
            {
                HttpContext.Current.Session[key] = value;
            }
        }

        public void Remove(string key)
        {
            HttpContext.Current.Session.Remove(key);
        }

        public string TryGet(string key)
        {
            return TryGet(key, () => string.Empty);
        }

        public string TryGet(string key, Func<string> getDefault)
        {
            var value = RetrieveValueWithDefaultHelper.TryGet<object>(() => this[key], getDefault);

            return (value == null) ? string.Empty: value.ToString();
        }

        public void SetTimeout(int timeoutSeconds)
        {
            HttpContext.Current.Session.Timeout = timeoutSeconds;
        }

        public void Clear()
        {
            HttpContext.Current.Session.Clear();
        }
    }
}

